/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package client.beans;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.faces.bean.ManagedBean;
import javax.xml.bind.JAXBException;
import server.tools.XMLRequester;
import server.tools.XmlHelper;
import types.RequestXML;
import types.RequestXML.Params;
import types.RequestXML.Params.Param;
import types.ResponseXML;

/**
 *
 * @author efim
 */
@Stateless
@ManagedBean
public class ServerBean {
    @EJB
    private AuthBean authBean;
    @EJB
    private XMLRequester xMLRequester;
    
    public Object queryToServer(String methodName,Map<String,String> params) throws JAXBException, IOException
    {
        InputStream is = null;
        RequestXML xml = new RequestXML();
        xml.setMethod(methodName);
        Params ps = new Params();
        for (String k : params.keySet()) {
            ps.addParam(new Param(k, params.get(k)));
        }
        ps.addParam(new Param("access_token", authBean.getAccessToken()));
        xml.setParams(ps);
        is = XmlHelper.Marshall(xml);
        Object data = XmlHelper.Unmarshall(ResponseXML.class, xMLRequester.Request(is));
        is.close();
        return data;
    }
    
    public Object queryAccessToken(String email,String password) throws JAXBException
    {        
        Object data = XmlHelper.Unmarshall(ResponseXML.class, xMLRequester.getAccessToken(email, password));
        return data;
    }
    
    public Object registrationUser(Map<String,String> params) throws JAXBException
    {        
        Object data = XmlHelper.Unmarshall(ResponseXML.class, xMLRequester.registration(params));
        return data;
    }
    
    
}
